<?php  

    function calculaNuevos($arch, $tbspc, $tbl)
    { 
        foreach ($arch->p1 as $x)
        {
            if(strcmp($x[0],$tbspc) == 0 && strcmp($x[1],$tbl) == 0)
                return $x[3];
        } 
        return NULL;
    }

    function calls($rl, $lst)
    {     
        $json = Array("p1" => Array());    
        $nuevos_viejos = false;
        //$archivo = fopen("json_graficos.json", "r+"); 
        $string = file_get_contents("json_graficos.json");  
        if($string)
        {
            $nuevos_viejos = TRUE;
            $arch_json = json_decode($string); 
        }
        //fclose($archivo); 
       // echo implode($hhehe->p1[0]);
        require("./Conn.php"); 
        $listHistorialesAux = Array("fecha" => "", "TABLE_NAME" => "", "TABLESPACE_NAME" => "", "NUM_ROWS" => 0,
        "MB" => 0, "nuevos" => 0);
        if (!$conn) 
        {
            $m = oci_error();
            trigger_error(htmlentities($m['message']), E_USER_ERROR);
        }
        $curs = oci_new_cursor($conn);
        $stid = oci_parse($conn, "begin PRC001(:cursorAux); end;");
        oci_bind_by_name($stid, ":cursorAux", $curs, -1, OCI_B_CURSOR);
        oci_execute($stid);
        oci_execute($curs);  // Execute the REF CURSOR like a normal statement id 
        $i =0; 
        while ($row = oci_fetch_array($curs, OCI_ASSOC)) 
        {   
            $listHistorialesAux["fecha"] = date("Y-m-d H:i:s");
            $listHistorialesAux["TABLE_NAME"] = $row["TABLE_NAME"];
            $listHistorialesAux["TABLESPACE_NAME"] = $row["TABLESPACE_NAME"];   
            $MB = $row['MB'][0];
            $n_row = isset($row['NUM_ROWS']) ? $row['NUM_ROWS'] : "0";  
            $listHistorialesAux["NUM_ROWS"] = $n_row;  
            if(!$nuevos_viejos)
                $listHistorialesAux["nuevos"] = 0; 
            else
            {
                $calc = calculaNuevos($arch_json,$listHistorialesAux["TABLESPACE_NAME"], $listHistorialesAux["TABLE_NAME"]);  
                if($calc != NULL) 
                {
                    $listHistorialesAux["nuevos"] = (int)$calc -  (int)$listHistorialesAux["NUM_ROWS"];
                }
                else 
                    $listHistorialesAux["nuevos"] =0;    
            }
            $MB_ = ($MB == ".")?(float)("0".$row['MB']):(float)$row['MB'];
            $listHistorialesAux['MB'] = $MB_; 
            array_push($lst, $listHistorialesAux);   
            array_push($json['p1'], [$listHistorialesAux["TABLESPACE_NAME"],$listHistorialesAux["TABLE_NAME"],$listHistorialesAux['MB'],$listHistorialesAux["NUM_ROWS"]]); 
            $i++; 
        }            
        if ($i > 0) 
        {  
            foreach ($lst as $v_)
            {  
                $rl->agrega($v_['TABLESPACE_NAME'], $v_['TABLE_NAME'], $v_['NUM_ROWS'], $v_['MB'] );
            }  
            CreaXML($lst); 
            $curs0 = oci_new_cursor($conn);
            $stid0 = oci_parse($conn, "begin PRC002(:cursorAux); end;");
            oci_bind_by_name($stid0, ":cursorAux", $curs0, -1, OCI_B_CURSOR);
            oci_execute($stid0);
            oci_execute($curs0);  // Execute the REF CURSOR like a normal statement id 
            while ($row = oci_fetch_array($curs0, OCI_ASSOC)) 
            {  
                $rl->setEspacios($row['Tablespace'], $row['Free MB'], $row['Used MB']);  
                
            }  
            $json_graficos = json_encode($json);  
            $archivo_json_graficos = fopen("json_graficos.json", "w+");
            fwrite($archivo_json_graficos, $json_graficos); 
            fclose($archivo_json_graficos); 
        }
    } 
